﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PrettyFormat
{
  /// <summary>
  /// Summary description for FileSizeFormatProviderTest
  /// </summary>
  [TestClass]
  public class FileSizeFormatProviderTest
  {
    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext { get; set; }

    [TestInitialize]
    public void Initialize()
    {
      var cultureInfo = new CultureInfo("en-US", false)
                          {
                            NumberFormat =
                              {
                                NumberDecimalSeparator = ".",
                                NumberGroupSeparator = "",
                                NumberDecimalDigits = 2,
                                NumberNegativePattern = 1
                              }
                          };

      Thread.CurrentThread.CurrentCulture =
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }

    [TestMethod]
    public void DefaultFormatTest()
    {
      var values = new Dictionary<decimal, string>
                     {
                       { 0.654M, "0.65 bytes"},
                       { 5.122M, "5.12 bytes"},
                       { 10M, "10.00 bytes" },
                       { 1024M, "1.00 KB" },
                       { 500.678M*1024M, "500.68 KB" },
                       {  23.2M*1024M*1024M, "23.20 MB" },
                       {  15M*1024M*1024M*1024M, "15.00 GB"},
                       { 800M*1024M*1024M*1024M*1024M, "800.00 TB" },
                       {   7M*1024M*1024M*1024M*1024M*1024M, "" + 7*1024 + ".00 TB"}
                     };

      var formatProvider = new FileSizeFormatProvider(new CultureInfo("en-US", false));

      values.ToList().ForEach(pair =>
                              Assert.AreEqual(pair.Value,
                                              string.Format(formatProvider, "{0:fs}", pair.Key),
                                              "without format specifier"));

      values.ToList().ForEach(pair =>
                              Assert.AreEqual(pair.Value,
                                              string.Format(formatProvider, "{0:fsF2}", pair.Key),
                                              "with format specifier: F2"));
    }

    
    [TestMethod]
    public void DefaultFormatTest_Culture_transparent()
    {
      var values = new Dictionary<decimal, string>
                     {
                       {0.654M, string.Format("{0:F2} bytes", 0.654M)},
                       {5.122M, string.Format("{0:F2} bytes", 5.122M)},
                       {10M, string.Format("{0:F2} bytes", 10M)},
                       {1024M, string.Format("{0:F2} KB", 1M)},
                       {500.678M*1024M, string.Format("{0:F2} KB", 500.68)},
                       {23.2M*1024M*1024M, string.Format("{0:F2} MB", 23.20M)},
                       {15M*1024M*1024M*1024M, string.Format("{0:F2} GB", 15.004M)},
                       {800M*1024M*1024M*1024M*1024M, string.Format("{0:F2} TB", 800M)},
                       {7M*1024M*1024M*1024M*1024M*1024M, string.Format("{0:F2} TB", 7168M)}
                     };

      var formatProvider = new FileSizeFormatProvider();

      values.ToList().ForEach(pair =>
                              Assert.AreEqual(pair.Value,
                                              string.Format(formatProvider, "{0:fs}", pair.Key),
                                              "without format specifier"));

      values.ToList().ForEach(pair =>
                              Assert.AreEqual(pair.Value,
                                              string.Format(formatProvider, "{0:fsF2}", pair.Key),
                                              "with format specifier: F2"));
    }

    
    [TestMethod]
    public void CustomFormatTest()
    {
      var values = new Dictionary<decimal, string>
                     {
                       { 5.122M, "5.12 bytes"},
                       { 10M, "10 bytes" },
                       { 1024M, "1 KB" },
                       { 500.1M*1024M, "500.1 KB" },
                     };

      var formatProvider = new FileSizeFormatProvider(new CultureInfo("en-US"));

      values.ToList().ForEach(pair =>
                              Assert.AreEqual(pair.Value,
                                              string.Format(formatProvider, "{0:fs######0.##}", pair.Key),
                                              "with custom format specifier"));
    }

   
  }
}
